import flash.external.ExternalInterface;

import gugga.utils.DebugUtils;

/**
 * @author branimir
 */
class gugga.utils.BrowserCommunicationUtility 
{
	public static function openUrl(aUrl : String, aTarget : String) : Void
	{
		if(aUrl.indexOf("javascript:") == 0)
		{
			var functionCallExpression : String = aUrl.substring(11);
			makeWeakJSFunctionCallByExpression(functionCallExpression);
		}
		else
		{
			getURL(aUrl, aTarget);
		}
	}
	
	public static function makeWeakJSFunctionCall(aFunctionName : String, aArguments : Array) : Void
	{
		var functionCallExpression : String = aFunctionName + "(";
		for (var i : Number = 0; i < aArguments.length; i++)
		{
			var arg : Object = aArguments[i];
			if(arg instanceof Number)
			{
				functionCallExpression += arg.toString();
			}
			else
			{
				functionCallExpression += ("'" + arg.toString() + "'");
			}
	
			if(i < aArguments.length - 1)
			{
				functionCallExpression += ",";
			}
		}
		functionCallExpression += ");";
		
		makeWeakJSFunctionCallByExpression(functionCallExpression);
	}
	
	public static function makeWeakJSFunctionCallByExpression(aFunctionCallExpression : String) : Void
	{
		if(ExternalInterface.available)
		{
			ExternalInterface.call("window.setTimeout", aFunctionCallExpression, 10);
		}
		else
		{
			getURL("javascript:" + aFunctionCallExpression);
		}	
	}
}